<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>原型与继承 | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/frontend_js基础_原型与继承.md.BUyJz8N1.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>js基础知识</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js基础/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>js基础知识</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/DOM.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>DOM</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Map.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Map</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Promise.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Promise</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Set.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Set</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/Symbol.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Symbol</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E4%BA%8B%E4%BB%B6.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>事件</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E4%BB%BB%E5%8A%A1%E7%AE%A1%E7%90%86.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>任务管理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>函数进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E7%BB%A7%E6%89%BF.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>原型与继承</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>基本类型</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%AF%B9%E8%B1%A1.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>对象</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>常用技巧</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>数组类型</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%A8%A1%E5%9D%97%E8%AE%BE%E8%AE%A1.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>模块设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>正则表达式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E7%A9%BA%E9%97%B4%E5%9D%90%E6%A0%87.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>空间坐标</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E7%B1%BB.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>类</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/frontend/js%E5%9F%BA%E7%A1%80/%E8%BF%AD%E4%BB%A3%E5%99%A8.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>迭代器</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _frontend_js%E5%9F%BA%E7%A1%80_%E5%8E%9F%E5%9E%8B%E4%B8%8E%E7%BB%A7%E6%89%BF" data-v-39a288b8><div><h1 id="原型与继承" tabindex="-1">原型与继承 <a class="header-anchor" href="#原型与继承" aria-label="Permalink to &quot;原型与继承&quot;">​</a></h1><h2 id="原型对象" tabindex="-1">原型对象 <a class="header-anchor" href="#原型对象" aria-label="Permalink to &quot;原型对象&quot;">​</a></h2><p>每个对象都有一个原型<code>prototype</code>对象，通过函数创建的对象也将拥有这个原型对象。原型是一个指向对象的指针。</p><ul><li>原型包含 <code>constructor</code> 属性，指向构造函数</li><li>对象包含 <code>__proto__</code> 指向他的原型对象</li></ul><p>默认情况下创建的对象都有原型</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> { </span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;"> };</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>以下 x、y 的原型都为元对象 Object，即 JS 中的根对象</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> x</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> y</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">x</span><span style="color:#ABB2BF;">) </span><span style="color:#56B6C2;">==</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">y</span><span style="color:#ABB2BF;">)); </span><span style="color:#7F848E;font-style:italic;">//true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>极简对象（纯数据字典对象）没有原型（原型为 null)</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#D19A66;">null</span><span style="color:#ABB2BF;">, {</span></span>
<span class="line"><span style="color:#E06C75;">    name</span><span style="color:#ABB2BF;">: {</span></span>
<span class="line"><span style="color:#E06C75;">        value</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;love&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"><span style="color:#ABB2BF;">})</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">xj</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//{name: &#39;love&#39;}</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">xj</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hasOwnProperty</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;name&quot;</span><span style="color:#ABB2BF;">)) </span><span style="color:#7F848E;font-style:italic;">//Error</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="关系图" tabindex="-1">关系图 <a class="header-anchor" href="#关系图" aria-label="Permalink to &quot;关系图&quot;">​</a></h4><p><img src="https://gitlab.com/loveagri/pic/-/raw/main/2024-12-06/12/18341_proto-link.png" alt="proto-link" loading="lazy"></p><p>下面使用 <code>setPrototypeOf</code> 与 <code>getPrototypeOf</code> 获取与设置原型</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> parent</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> { </span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;parent&quot;</span><span style="color:#ABB2BF;"> };</span></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">setPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">parent</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">));</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>constructor 存在于 prototype 原型中，用于指向构建函数的引用。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> hd</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#98C379;"> &quot;show method&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  };</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> obj</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> hd</span><span style="color:#ABB2BF;">(); </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">obj</span><span style="color:#C678DD;"> instanceof</span><span style="color:#E5C07B;"> hd</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> obj2</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#E5C07B;"> obj</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">constructor</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">dir</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">obj2</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#ABB2BF;">()); </span><span style="color:#7F848E;font-style:italic;">//show method</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>使用对象的 <code>constructor</code> 创建对象，即构造函数的的原型可以通过实例对象的反向找到。函数默认<code>prototype</code> 指包含一个属性 <code>constructor</code> 的对象，<code>constructor</code> 指向当前构造函数</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">age</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> age</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> createByObject</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">obj</span><span style="color:#ABB2BF;">, ...</span><span style="color:#E06C75;font-style:italic;">args</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#C678DD;">  const</span><span style="color:#E5C07B;"> constructor</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">obj</span><span style="color:#ABB2BF;">).</span><span style="color:#E06C75;">constructor</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#C678DD;">  return</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> constructor</span><span style="color:#ABB2BF;">(...</span><span style="color:#E06C75;">args</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;zhangsan&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#61AFEF;"> createByObject</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;lisi&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">12</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">xj</span><span style="color:#ABB2BF;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>综上：</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;zhangsan&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">xj</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">constructor</span><span style="color:#56B6C2;"> ==</span><span style="color:#E06C75;"> User</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//true 构造函数原型的constructor就是构造函数本身</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">xj</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#56B6C2;"> ==</span><span style="color:#E5C07B;"> User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#E5C07B;"> xj</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">constructor</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;李四&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#56B6C2;"> ==</span><span style="color:#E5C07B;"> xj</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="原型检测" tabindex="-1">原型检测 <a class="header-anchor" href="#原型检测" aria-label="Permalink to &quot;原型检测&quot;">​</a></h3><p>instanceof 检测构造函数的 <code>prototype</code> 属性是否出现在某个实例对象的原型链上</p><p>使用<code>isPrototypeOf</code>检测一个对象是否是另一个对象的原型链中</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> a</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> b</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> c</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">setPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">a</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">b</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">setPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">b</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">c</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">b</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">isPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">a</span><span style="color:#ABB2BF;">)); </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">c</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">isPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">a</span><span style="color:#ABB2BF;">)); </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">c</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">isPrototypeOf</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">b</span><span style="color:#ABB2BF;">)); </span><span style="color:#7F848E;font-style:italic;">//true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="属性遍历" tabindex="-1">属性遍历 <a class="header-anchor" href="#属性遍历" aria-label="Permalink to &quot;属性遍历&quot;">​</a></h3><p>使用<code>in</code> 检测原型链上是否存在属性，使用 <code>hasOwnProperty</code> 只检测当前对象。</p><p>使用 <code>for/in</code> 遍历时同时会遍历原型上的属性如下例</p><p><code>hasOwnProperty</code> 方法判断对象是否存在属性，而不会查找原型。</p><h3 id="借用原型" tabindex="-1">借用原型 <a class="header-anchor" href="#借用原型" aria-label="Permalink to &quot;借用原型&quot;">​</a></h3><p>使用 <code>call</code> 或 <code>apply</code> 可以借用其他原型方法完成功能。</p><h3 id="this" tabindex="-1">this <a class="header-anchor" href="#this" aria-label="Permalink to &quot;this&quot;">​</a></h3><p><code>this</code> 不受原型继承影响，<code>this</code> 指向调用属性时使用的对象。</p><h2 id="原型总结" tabindex="-1">原型总结 <a class="header-anchor" href="#原型总结" aria-label="Permalink to &quot;原型总结&quot;">​</a></h2><h3 id="prototype" tabindex="-1">prototype <a class="header-anchor" href="#prototype" aria-label="Permalink to &quot;prototype&quot;">​</a></h3><p><code>prototype</code>是函数对象独有的属性，它指向了一个原型对象。每个函数对象都有一个<code>prototype</code>属性，但它只有在这个函数作为构造函数使用时才有意义。</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> Person</span><span style="color:#ABB2BF;">() {}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">Person</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// 输出：Person {}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>prototype</code>属性的主要作用是在构造函数模式下，用于构建<strong>实例对象</strong>的原型链。当我们使用构造函数来创建一个<strong>实例对象</strong>时，它的<code>__proto__</code>属性会指向构造函数的<code>prototype</code>属性。</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> person</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> Person</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">person</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#56B6C2;"> ===</span><span style="color:#E5C07B;"> Person</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// 输出：true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的代码中，我们使用了Person构造函数来创建了一个对象person。结果表明，<code>person.__proto__</code>指向了<code>Person.prototype</code>。</p><h3 id="object-create" tabindex="-1">Object.create <a class="header-anchor" href="#object-create" aria-label="Permalink to &quot;Object.create&quot;">​</a></h3><p>使用<code>Object.create</code>创建一个新对象时使用现有对象做为新对象的原型对象，第二个参数设置新对象的属性</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> user</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">  show</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">user</span><span style="color:#ABB2BF;">, {</span></span>
<span class="line"><span style="color:#E06C75;">  name</span><span style="color:#ABB2BF;">: {</span></span>
<span class="line"><span style="color:#E06C75;">    value</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;you&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">});</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;love&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#ABB2BF;">());</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="proto" tabindex="-1"><strong>proto</strong> <a class="header-anchor" href="#proto" aria-label="Permalink to &quot;**proto**&quot;">​</a></h3><p><code>__proto__</code>是对象的一个内置属性，它用于指向该对象的原型。<strong>每个</strong>对象都有一个<code>__proto__</code>属性，包括自定义对象、内置对象和函数对象。通过<code>__proto__</code>属性，我们可以访问和操作对象的原型链。</p><div class="language-js line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> obj</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">obj</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// 输出：Object {}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> arr</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> [];</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">arr</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// 输出：Array []</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> func</span><span style="color:#ABB2BF;">() {}</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">func</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// 输出：[Function]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面的代码中，我们创建了一个空对象obj，并访问了它的<code>__proto__</code>属性。可以看到，<code>obj.__proto__</code>指向了一个Object{}对象。同样，我们还创建了一个空数组arr，并访问了它的<code>__proto_</code>_属性，结果是<code>arr.__proto__</code>指向了一个Array []对象。而对于函数对象func来说，它的<code>__proto_</code>_指向的是一个[Function]对象。</p><p>总结起来，<code>__proto__</code>属性用于指向对象的原型，我们可以通过它来访问和操作原型链。</p><p>在实例化对象上存在 <strong>proto</strong> 记录了原型，所以可以通过对象访问到原型的属性或方法。</p><ul><li><code>__proto__</code> 不是对象属性，理解为<code>prototype</code> 的 <code>getter/setter</code> 实现，他是一个非标准定义</li><li><code>__proto__</code> 内部使用<code>getter/setter</code> 控制值，所以只允许对象或 null</li><li>建议使用 <code>Object.setPrototypeOf</code> 与<code>Object.getProttoeypOf</code> 替代 <code>__proto__</code></li></ul><p>下面修改对象的 <code>__proto__</code> 是不会成功的，因为<code>_proto__</code> 内部使用<code>getter/setter</code> 控制值，所以只允许对象或 null</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {};</span></span>
<span class="line"><span style="color:#E5C07B;">xj</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;love&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">xj</span><span style="color:#ABB2BF;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>下面定义的<code>__proto__</code> 就会成功，因为这是一个极简对象，没有原型对象所以不会影响<code>__proto__</code>赋值。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#D19A66;">null</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;love&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">hd</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//{__proto__: &quot;love&quot;}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>__proto__</code>和prototype都与对象的原型相关，它们之间的联系和区别如下：</p><ol><li><p><code>__proto__</code>是实例对象的属性，用于指向该对象的原型；而prototype是构造函数的属性，用于指向构造函数的原型对象。</p></li><li><p><code>__proto_</code>_是读取并访问对象的原型链的属性，可以在实例对象上直接访问；而prototype是构造函数的属性，只能在构造函数内部访问。</p></li><li><p><code>__proto__</code>是非标准的属性，只有部分浏览器支持；而prototype是标准属性，所有的对象和函数都有。</p></li><li><p><code>__proto__</code>是获取对象原型的，<code>prototype</code>是为对象添加原型的属性和方法的。</p></li></ol><p>‌<strong>实例与原型的关系</strong>‌：当一个对象被创建时，它的<code>__proto__</code>属性会指向其构造函数的<code>prototype</code>属性。这意味着每个对象的<code>__proto__</code>属性实际上就是其构造函数的<code>prototype</code>属性‌。例如，如果有一个构造函数<code>Person</code>，创建了一个实例<code>person1</code>，那么<code>person1.__proto__ === Person.prototype</code>将会返回<code>true</code></p><p>原型中保存引用类型会造成对象共享属性，所以一般只会在原型中定义方法。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">() {}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#E06C75;">  lessons</span><span style="color:#ABB2BF;">: [</span><span style="color:#98C379;">&quot;JS&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;VUE&quot;</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> wangwu</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">lessons</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">push</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;CSS&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">lessons</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//[&quot;JS&quot;, &quot;VUE&quot;, &quot;CSS&quot;]</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">wangwu</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">lessons</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//[&quot;JS&quot;, &quot;VUE&quot;, &quot;CSS&quot;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>为 Object 原型对象添加方法，将影响所有函数</p><div class="language-html line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">html</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">body</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">  &lt;</span><span style="color:#E06C75;">button</span><span style="color:#D19A66;"> onclick</span><span style="color:#ABB2BF;">=</span><span style="color:#98C379;">&quot;</span><span style="color:#E5C07B;">this</span><span style="color:#98C379;">.</span><span style="color:#61AFEF;">hide</span><span style="color:#98C379;">()&quot;</span><span style="color:#ABB2BF;">&gt;love&lt;/</span><span style="color:#E06C75;">button</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">body</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span>
<span class="line"><span style="color:#E5C07B;">  Object</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hide</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">    this</span><span style="color:#ABB2BF;">.</span><span style="color:#E5C07B;">style</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">display</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;none&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  };</span></span>
<span class="line"><span style="color:#ABB2BF;">&lt;/</span><span style="color:#E06C75;">script</span><span style="color:#ABB2BF;">&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>对象设置属性，只是修改对象属性并不会修改原型属性，使用<code>hasOwnProperty</code> 判断对象本身是否含有属性并不会检测原型。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">() {}</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> wangwu</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;小明&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hasOwnProperty</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;name&quot;</span><span style="color:#ABB2BF;">));</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//修改原型属性后</span></span>
<span class="line"><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;张三&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">wangwu</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//删除对象属性后</span></span>
<span class="line"><span style="color:#C678DD;">delete</span><span style="color:#E5C07B;"> lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hasOwnProperty</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;name&quot;</span><span style="color:#ABB2BF;">));</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">);</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>使用 <code>in</code> 会检测原型与对象，而 <code>hasOwnProperty</code> 只检测对象，所以结合后可判断属性是否在原型中</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;后盾人&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//in会在原型中检测</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;name&quot;</span><span style="color:#C678DD;"> in</span><span style="color:#E06C75;"> lisi</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//hasOwnProperty 检测对象属性</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hasOwnProperty</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;name&quot;</span><span style="color:#ABB2BF;">));</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="使用建议" tabindex="-1">使用建议 <a class="header-anchor" href="#使用建议" aria-label="Permalink to &quot;使用建议&quot;">​</a></h3><p>通过前介绍我们知道可以使用多种方式设置原型，下面是按时间顺序的排列</p><ol><li><code>prototype</code> 构造函数的原型属性</li><li><code>Object.create</code> 创建对象时指定原型</li><li><code>__proto__</code> 声明自定义的非标准属性设置原型，解决之前通过 <code>Object.create</code> 定义原型，而没提供获取方法</li><li><code>Object.setPrototypeOf</code> 设置对象原型</li></ol><p>这几种方式都可以管理原型，一般来讲使用 <code>prototype</code> 更改构造函数原型，使用 <code>Object.setPrototypeOf</code> 与 <code>Object.getPrototypeOf</code> 获取或设置原型。</p><h2 id="构造函数" tabindex="-1">构造函数 <a class="header-anchor" href="#构造函数" aria-label="Permalink to &quot;构造函数&quot;">​</a></h2><h3 id="使用优化" tabindex="-1">使用优化 <a class="header-anchor" href="#使用优化" aria-label="Permalink to &quot;使用优化&quot;">​</a></h3><p>使用构造函数会产生函数复制造成内存占用，及函数不能共享的问题。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">get</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  };</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;小明&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> wangwu</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;王五&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">get</span><span style="color:#56B6C2;"> ==</span><span style="color:#E5C07B;"> wangwu</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">get</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">//false</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//优化后</span></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">    this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">get</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;"> () {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;小明&quot;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> wangwu</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;王五&quot;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">get</span><span style="color:#56B6C2;"> ==</span><span style="color:#E5C07B;"> wangwu</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">get</span><span style="color:#ABB2BF;">) </span><span style="color:#7F848E;font-style:italic;">//true</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">//通过修改原型方法会影响所有对象调用，因为方法是共用的</span></span>
<span class="line"><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">get</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;"> () {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#98C379;"> &quot;love&quot;</span><span style="color:#56B6C2;"> +</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">())</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">wangwu</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">());</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p>使用<code>Object.assign</code>一次设置原型方法来复用，后面会使用这个功能实现 Mixin 模式</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">age</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> age</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">assign</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">, {</span></span>
<span class="line"><span style="color:#61AFEF;">  getName</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">      return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  },</span></span>
<span class="line"><span style="color:#61AFEF;">  getAge</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">      return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">});</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> lisi</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;李四&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">12</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xiaoming</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;小明&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">32</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getName</span><span style="color:#ABB2BF;">()); </span><span style="color:#7F848E;font-style:italic;">//李四</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">lisi</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">__proto__</span><span style="color:#ABB2BF;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>我们希望调用父类构造函数完成对象的属性初始化，但像下面这样使用是不会成功的。因为此时 <code>this</code> 指向了 window，无法为当前对象声明属性。</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">);</span><span style="color:#7F848E;font-style:italic;">// Window</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getUserName</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">  return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#61AFEF;">  User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">Admin</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">Admin</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">role</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">xj</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getUserName</span><span style="color:#ABB2BF;">()); </span><span style="color:#7F848E;font-style:italic;">//undefined</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>解决上面的问题是使用 <code>call/apply</code> 为每个生成的对象设置属性</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">); </span><span style="color:#7F848E;font-style:italic;">// Admin</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getUserName</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">  return</span><span style="color:#E5C07B;"> this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  User</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">call</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">Admin</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> xj</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;love&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">xj</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">getUserName</span><span style="color:#ABB2BF;">()); </span><span style="color:#7F848E;font-style:italic;">//love</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>Mixin 模式</p><p><code>JS</code>不能实现多继承，如果要使用多个类的方法时可以使用<code>mixin</code>混合模式来完成。</p><p>下面分拆功能使用 Mixin 实现多继承，使用代码结构更清晰。只让 <code>Admin</code> 继承 <code>User</code> 原型</p><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> extend</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">sub</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">sup</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  sub</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">sup</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">  sub</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">constructor</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> sub</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">age</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> age</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> Credit</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">  total</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;统计积分&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> Request</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">  ajax</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;请求后台&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(...</span><span style="color:#E06C75;font-style:italic;">args</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  User</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">apply</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">args</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#61AFEF;">extend</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">Admin</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">User</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">assign</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">Admin</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">Request</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">Credit</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;向军&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">19</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">total</span><span style="color:#ABB2BF;">(); </span><span style="color:#7F848E;font-style:italic;">//统计积分</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">ajax</span><span style="color:#ABB2BF;">(); </span><span style="color:#7F848E;font-style:italic;">//请求后台</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><p><code>mixin</code> 类也可以继承其他类，比如下面的 <code>Create</code> 类获取积分要请求后台，就需要继承 <code>Request</code> 来完成。</p><ul><li><code>super</code> 是在 <code>mixin</code> 类的原型中查找，而不是在 <code>User</code> 原型中</li></ul><div class="language-javascript line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> extend</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">sub</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">sup</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  sub</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#56B6C2;"> =</span><span style="color:#E5C07B;"> Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">create</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">sup</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">  sub</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">constructor</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> sub</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> User</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">age</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> name</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#E5C07B;">  this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#56B6C2;"> =</span><span style="color:#E06C75;"> age</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#E5C07B;">User</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> function</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">  console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">name</span><span style="color:#ABB2BF;">, </span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">age</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> Request</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">  ajax</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#98C379;"> &quot;请求后台&quot;</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"><span style="color:#C678DD;">const</span><span style="color:#E5C07B;"> Credit</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line highlighted"><span style="color:#E06C75;">  __proto__</span><span style="color:#ABB2BF;">: </span><span style="color:#E06C75;">Request</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#61AFEF;">  total</span><span style="color:#ABB2BF;">() {</span></span>
<span class="line"><span style="color:#E5C07B;">    console</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">log</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;font-style:italic;">super</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">ajax</span><span style="color:#ABB2BF;">() </span><span style="color:#56B6C2;">+</span><span style="color:#98C379;"> &quot;,统计积分&quot;</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">};</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">function</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(...</span><span style="color:#E06C75;font-style:italic;">args</span><span style="color:#ABB2BF;">) {</span></span>
<span class="line"><span style="color:#E5C07B;">  User</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">apply</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">this</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">args</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span>
<span class="line"><span style="color:#61AFEF;">extend</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">Admin</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">User</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">Object</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">assign</span><span style="color:#ABB2BF;">(</span><span style="color:#E5C07B;">Admin</span><span style="color:#ABB2BF;">.</span><span style="color:#E06C75;">prototype</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">Request</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;">Credit</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#C678DD;">let</span><span style="color:#E06C75;"> hd</span><span style="color:#56B6C2;"> =</span><span style="color:#C678DD;"> new</span><span style="color:#61AFEF;"> Admin</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&quot;you&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">19</span><span style="color:#ABB2BF;">);</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">show</span><span style="color:#ABB2BF;">();</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">total</span><span style="color:#ABB2BF;">(); </span><span style="color:#7F848E;font-style:italic;">//统计积分</span></span>
<span class="line"><span style="color:#E5C07B;">hd</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">ajax</span><span style="color:#ABB2BF;">(); </span><span style="color:#7F848E;font-style:italic;">//请求后台</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>函数进阶</span><!--]--></a></div><div class="pager" data-v-e257564d><a class="VPLink link pager-link next" href="/frontend/js%E5%9F%BA%E7%A1%80/%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>下一篇</span><span class="title" data-v-e257564d>基本类型</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>